home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / tn3270 / util.c < prev    next >
Text File  |  1992-04-18  |  13KB  |  582 lines

  1. /*
  2.  *  tn3270 for the Macintosh Source Code
  3.  *  Brown University Computing and Information Services
  4.  *  Version 2.4d7  April, 1992
  5.  *  Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
  6.  *  Peter John DiCamillo.
  7.  *
  8.  *  Permission is granted to any individual or institution to use, copy,
  9.  *  or redistribute the binary version of this software and its
  10.  *  documentation provided this notice and the copyright notices are
  11.  *  retained.  Permission is granted to any individual or non-profit
  12.  *  institution to use, copy, modify, or redistribute the source files
  13.  *  of this software provided this notice and the copyright notices are
  14.  *  retained.  This software may not be distributed for profit, either
  15.  *  in original form or in derivative works, nor can the source be
  16.  *  distributed to other than an individual or a non-profit institution.
  17.  *  Any  individual or group interested in seeing and/or using these
  18.  *  source files but who are prevented from doing so by the above
  19.  *  constraints should contact Don Wolfe, Assistant Vice-President for
  20.  *  Computer Systems at Brown University, (401) 863-7250, for possible
  21.  *  software licensing of the source developed at Brown.
  22.  *
  23.  *  Brown University and Peter John DiCamillo make no representations
  24.  *  about the suitability of this software for any purpose.
  25.  *
  26.  *  BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
  27.  *  EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
  28.  *  INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
  29.  *  WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  */
  32.  
  33. #if !defined(USEDUMP)
  34.     #include "maclib.h"
  35.     #include "termdef.h"
  36.     #include "tn3270funcs.h"
  37.     #include "globals.h"
  38. #else
  39.     #pragma load "tn3270DumpFile"
  40. #endif
  41.  
  42. #pragma segment 3270seg2
  43.  
  44. extern char smallscreen;        /* complete 24 x 80 window doesn't fit */
  45.  
  46. #define    IAC    255        /* Telnet protocol interpret as command: */
  47.  
  48. #define DEBUGFILE    "\pRam:tcpdebug"
  49. #define DEBUGVOL    "\pRam"
  50.                                 /* for debugging via serial port */
  51. static IOParam pbo;                    /* serial output parms */
  52. static unsigned char pbchr;            /* output character */
  53. static char debugopnflg = 0;
  54.  
  55. void tcpmemcpy(unsigned char *dest, unsigned char *src,
  56.                short *len, char tcpflg)
  57. {
  58. register short i, copylen;
  59.  
  60. if (tcpflg) {
  61.     if ((*len) == 0) return;
  62.     copylen = 0;
  63.     for (i=0; i < (*len); i++) {
  64.         if (src[i] == IAC) {
  65.             dest[copylen++] = IAC;
  66.             dest[copylen++] = IAC;
  67.             }
  68.         else {
  69.             dest[copylen++] = src[i];
  70.             }
  71.         }
  72.     (*len) = copylen;
  73.     }
  74. else {
  75.     memcpy(dest, src, *len);
  76.     }
  77. }
  78.  
  79. void GetGlobalRect(WindowPtr window, Rect *globalRect)
  80. {
  81. GrafPtr gp;
  82.  
  83. GetPort(&gp);
  84. SetPort(window);
  85. (*globalRect) = window->portRect;
  86. LocalToGlobal((Point *)(&(globalRect->top)));
  87. LocalToGlobal((Point *)(&(globalRect->bottom)));
  88. SetPort(gp);
  89. }
  90.  
  91. void defxtab(unsigned char **xtabinfo, unsigned char *xtab)
  92. {
  93. Size infosize;
  94. register short i;
  95.  
  96. /* generate default table */
  97. for (i=0; i < 256; i++) {
  98.     xtab[i] = i;
  99.     }
  100.  
  101. /* modify specified values */
  102. infosize = GetHandleSize((Handle)xtabinfo); 
  103. /* ensure length is a multiple of two (as it should be) */
  104. infosize >>= 1;
  105. infosize <<= 1;
  106. if (infosize < 2) return;
  107. for (i=0; i < infosize; i += 2) {
  108.     xtab[(*xtabinfo)[i]] = (*xtabinfo)[i+1];
  109.     }
  110. }
  111.  
  112. void getdeskrect(Rect *r)
  113. {
  114. static RgnHandle *GrayRgn = (RgnHandle *)0x9ee;
  115.  
  116. (*r) = (**GrayRgn)->rgnBBox;
  117. }
  118.  
  119. unsigned char getmodel(short rows, short cols, cnr *cp)
  120. {
  121.                     /* note: for models 4 and 5, only 3278 is used, since
  122.                               there are no real 3279-4 or -5 terminals    */
  123. if (colormac && (!cp->cs.nocolor)) {
  124.     if ((rows == 32) && (cols == 80)) return(MDL32793);
  125.     else if ((rows == 43) && (cols == 80)) return(MDL32784);
  126.     else if ((rows == 27) && (cols == 132)) return(MDL32785);
  127.     else return(MDL32792);
  128.     }
  129. else {
  130.     if ((rows == 32) && (cols == 80)) return(MDL32783);
  131.     else if ((rows == 43) && (cols == 80)) return(MDL32784);
  132.     else if ((rows == 27) && (cols == 132)) return(MDL32785);
  133.     else return(MDL32782);
  134.     }
  135. }
  136.  
  137. void adjmove(Rect *wr)
  138.                 /* wr is the rectangle for a window in global coordinates */
  139.                 /* the position of the rectangle is adjusted to fit on 
  140.                    on the screen as well as possible */
  141. {
  142. Rect r, devrect;
  143. Point ctr;
  144. GDHandle devgdev;
  145. char found;
  146. char devok;
  147. short adj;
  148.  
  149.  
  150. r = *wr;            /* local copy of window rectange */
  151.                     /* get center point and corresponding device rectange */
  152. ctr.h = (r.left + r.right)/2;
  153. ctr.v = (r.top + r.bottom)/2;
  154. if (colormac) {
  155.     found = 0;
  156.     devgdev = GetDeviceList();
  157.     while (devgdev != 0) {
  158.         devrect = (*devgdev)->gdRect;
  159.         if (PtInRect(ctr, &devrect)) {
  160.             found = 1;
  161.             break;
  162.             }
  163.         else {
  164.             devgdev = GetNextDevice(devgdev);
  165.             }
  166.         }
  167.     if (!found) {
  168.         devgdev = GetMainDevice();
  169.         if (devgdev == 0) {
  170.             return;    /* shouldn't happen */
  171.             }
  172.         else {
  173.             devrect = (*devgdev)->gdRect;
  174.             }
  175.         }
  176.     }
  177. else {
  178.     devrect = qd.screenBits.bounds;
  179.     }
  180. /* adjust for menu bar */
  181. if ((devrect.top == 0) && (devrect.left == 0)) {
  182.     if (smallscreen) {
  183.         devrect.top = 4;
  184.         }
  185.     else {
  186.         devrect.top = 20;
  187.         }
  188.     }
  189.  
  190. /* return if window is entirely in device rectangle */
  191. if ((devrect.top <= r.top) && (devrect.bottom >= r.bottom) &&
  192.     (devrect.left <= r.left) && (devrect.right >= r.right)) {
  193.     return;
  194.     }
  195. if (colormac) {
  196.     /* check window will fit on device */
  197.     devok = ((devrect.bottom - devrect.top) >= (r.bottom - r.top)) &&
  198.              ((devrect.right - devrect.left) >= (r.right - r.left));
  199.     /* if not, use entire desktop */
  200.     if (!devok) {
  201.         getdeskrect(&devrect);
  202.         if (devrect.top == 0) {
  203.             if (smallscreen) {
  204.                 devrect.top = 4;
  205.                 }
  206.             else {
  207.                 devrect.top = 20;
  208.                 }
  209.             }
  210.         if ((devrect.top <= r.top) && (devrect.bottom >= r.bottom) &&
  211.             (devrect.left <= r.left) && (devrect.right >= r.right)) {
  212.             return;
  213.             }
  214.         }
  215.     }
  216.             /* following order ensures that top left corner is in window */
  217. /* make right edge fit */
  218. adj = r.right - devrect.right;
  219. if (adj > 0) {
  220.     r.right -= adj;
  221.     r.left -= adj;
  222.     }
  223.     
  224. /* make bottom edge fit */
  225. adj = r.bottom - devrect.bottom;
  226. if (adj > 0) {
  227.     r.bottom -= adj;
  228.     r.top -= adj;
  229.     }
  230.     
  231. /* make left edge fit */
  232. adj = devrect.left - r.left;
  233. if (adj > 0) {
  234.     r.left += adj;
  235.     r.right += adj;
  236.     }
  237.     
  238. /* make top edge fit */
  239. adj = devrect.top - r.top;
  240. if (adj > 0) {
  241.     r.top += adj;
  242.     r.bottom += adj;
  243.     }
  244.     
  245. (*wr) = r;
  246. }
  247.  
  248. void def_sessname(cnr *cp)
  249. {
  250. unsigned char newname[256];
  251. short i, myseq;
  252. cnr *listcp;
  253.  
  254. if (strlen(cp->cswtitle) > 0) {
  255.     strcpy(newname, cp->cswtitle);
  256.     }
  257. else if (strlen(cp->s_fName) > 0) {
  258.     strcpy(newname, cp->s_fName);
  259.     }
  260. else {
  261.     strcpy(newname, cp->hostonly);
  262.     }
  263. newname[249] = 0;        /* truncate if necessary */
  264. strcpy(cp->sessname, newname);
  265.  
  266. myseq = 0;
  267. for (i=0; i < MAXSESSIONS; i++) {
  268.     listcp = cplist[i];
  269.     if ((listcp != 0) && (listcp != cp)) {
  270.         if (strcmp(listcp->sessname, cp->sessname) == 0) {
  271.             if (listcp->sessnameseq == 0) {
  272.                 listcp->sessnameseq = 1;
  273.                 get_sessname(newname, listcp);
  274.                 c2pstr(newname);
  275.                 if (listcp->myWindow != 0) {
  276.                     SetWTitle(listcp->myWindow, newname);
  277.                     }
  278.                 myseq = 2;
  279.                 break;
  280.                 }
  281.             else {
  282.                 if (myseq < (listcp->sessnameseq+1)) {
  283.                     myseq = listcp->sessnameseq+1;
  284.                     }
  285.                 }
  286.             }
  287.  
  288.         }
  289.     }
  290. cp->sessnameseq = myseq;
  291. }
  292.  
  293. void get_sessname(unsigned char *sname, cnr *cp)
  294. {
  295. if (cp->sessnameseq == 0) {
  296.     strcpy(sname, cp->sessname);
  297.     }
  298. else {
  299.     sprintf(sname, "%s %d", cp->sessname, cp->sessnameseq);
  300.     }
  301. }
  302.  
  303. GDHandle myGetMaxDevice(Rect *globalRect)
  304. {
  305. GDHandle GDTemp;
  306.  
  307. GDTemp = GetMaxDevice(globalRect);
  308. if (GDTemp != 0) return(GDTemp);
  309. return(GetMainDevice());
  310. }
  311.  
  312. short cfmapsize(char which, char color)
  313. {
  314. short dflthsize, dfltvsize;
  315. short althsize, altvsize;
  316. short hpixsize, vpixsize;
  317. unsigned short rowBytes, bwsize, colorsize;
  318. long mapsize;
  319. Rect deskrect;
  320. GDHandle currGD;
  321. PixMapHandle currPM;
  322.  
  323. /* calculate maximum hpixsize and vpixsize */
  324. if (cf_dfltptsize == 9) {
  325.     dflthsize = 80 * 6 + 10;
  326.     dfltvsize = 24 * 12 + 26;
  327.     }
  328. else {
  329.     dflthsize = 80 * 7 + 10;
  330.     dfltvsize = 24 * 16 + 26;
  331.     }
  332. if (cf_altptsize == 9) {
  333.     althsize = cf_altcols * 6 + 10;
  334.     altvsize = cf_altrows * 12 + 26;
  335.     }
  336. else {
  337.     althsize = cf_altcols * 7 + 10;
  338.     altvsize = cf_altrows * 16 + 26;
  339.     }
  340. if (dflthsize > althsize) {
  341.     hpixsize = dflthsize;
  342.     }
  343. else {
  344.     hpixsize = althsize;
  345.     }
  346. if (dfltvsize > altvsize) {
  347.     vpixsize = dfltvsize;
  348.     }
  349. else {
  350.     vpixsize = altvsize;
  351.     }
  352.  
  353. if (which == 0) {                /* text bitmap for color */
  354.     if (!colormac) return(0);
  355.  
  356.     rowBytes = (((hpixsize + 31) >> 5) + 1) << 2;
  357.     mapsize = vpixsize;
  358.     mapsize = mapsize * rowBytes + 4;
  359.     bwsize = (mapsize + 1023) / 1024;
  360.  
  361.     getdeskrect(&deskrect);
  362.     currGD = myGetMaxDevice(&deskrect);
  363.     currPM = (*currGD)->gdPMap;
  364.     rowBytes = ((((hpixsize * (*currPM)->pixelSize) + 31) >> 5) + 1) << 2;
  365.     mapsize = vpixsize;
  366.     mapsize = mapsize * rowBytes + 4;
  367.     colorsize = (mapsize + 1023) / 1024;
  368.  
  369.     return(colorsize - bwsize);    
  370.     }
  371. else {                            /* graphics bitmap */
  372.     if (color && colormac) {
  373.         getdeskrect(&deskrect);
  374.         currGD = myGetMaxDevice(&deskrect);
  375.         currPM = (*currGD)->gdPMap;
  376.         rowBytes = ((((hpixsize * (*currPM)->pixelSize) + 31) >> 5) + 1) << 2;
  377.         mapsize = vpixsize - 19;
  378.         mapsize = mapsize * rowBytes + 4;
  379.         colorsize = (mapsize + 1023) / 1024;
  380.         return(colorsize);
  381.         }
  382.     else {
  383.         rowBytes = (((hpixsize + 31) >> 5) + 1) << 2;
  384.         mapsize = vpixsize - 19;
  385.         mapsize = mapsize * rowBytes + 4;
  386.         bwsize = (mapsize + 1023) / 1024;
  387.         return(bwsize);
  388.         }
  389.     }
  390. }
  391.  
  392. writeinfo *new_writeinfo(cnr *cp)
  393. {
  394. short i;
  395. writeinfo *w;
  396.  
  397. for (i=0; i < WRITEINFOCOUNT-1; i++) {
  398.     cp->writesave[i] = cp->writesave[i+1];
  399.     }
  400. w = &(cp->writesave[WRITEINFOCOUNT-1]);
  401. memset(w, 0, sizeof(writeinfo));
  402. cp->writesave[WRITEINFOCOUNT-1].seqnum =
  403.     cp->writesave[WRITEINFOCOUNT-2].seqnum + 1;
  404. return(w);
  405. }
  406.  
  407. void show_writeinfo(cnr *cp)
  408. {
  409. static unsigned long * Ticks = (unsigned long *)0x16a;
  410. char dbsave;
  411. writeinfo *w;
  412. unsigned long currticks, elapsed, duration;
  413. short i, j, datalength;
  414. unsigned char hexstr[8];
  415.  
  416. if (cp == 0) return;
  417. dbsave = cf_dblevel;
  418. cf_dblevel = 1;
  419.  
  420. if (writedebug == 0) {
  421.     writedebug = 1;
  422.     dbdlg("Write tracing now enabled.");
  423.     cf_dblevel = dbsave;
  424.     return;
  425.     }
  426.  
  427. currticks = *Ticks;
  428. for (i=0; i < WRITEINFOCOUNT; i++) {
  429.     w = &(cp->writesave[i]);
  430.     if (w->seqnum != 0) {
  431.         elapsed = (currticks - w->start_ticks + 30)/60;
  432.         duration = (w->end_ticks - w->start_ticks);
  433.         sprintf(dbgmsg, "#%d: %ld sec. ago for %ld ticks, write calls = %d, length = %d, start = \"",
  434.             w->seqnum, elapsed, duration, w->calls, w->length);
  435.         datalength = w->length;
  436.         if (datalength > INFODATASIZE) datalength = INFODATASIZE;
  437.         for (j=0; j < datalength; j++) {
  438.             if (strlen(dbgmsg) > 250) break;
  439.             if (j == 0) {
  440.                 sprintf(hexstr, "%x", w->data[j]);
  441.                 }
  442.             else {
  443.                 sprintf(hexstr, " %x", w->data[j]);
  444.                 }
  445.             strcat(dbgmsg, hexstr);
  446.             }
  447.         strcat(dbgmsg, "\"");
  448.         dbdlg(dbgmsg);
  449.         }
  450.     }
  451. cf_dblevel = dbsave;
  452. }
  453.  
  454. void putln (unsigned char *s)        /*  put message in error log */
  455. {
  456. static FILE *fp = 0;
  457. short fref, i;
  458. long count;
  459. static int rc;
  460.  
  461. if (!cf_dblevel) return;
  462.  
  463. if (cf_dblevel == 99) {
  464.     if (!debugopnflg) {
  465.         rc = seropen();
  466.         debugopnflg = 1;
  467.         }
  468.     if (rc != 0) return;
  469.  
  470.     count = strlen(s);
  471.     if (count > 0)
  472.         for (i = 0; i < count; i++) {
  473.             rc = serchr(s[i]);
  474.             if (havetcp) myStask();
  475.             if (rc != 0) break;
  476.             }
  477.     if (rc == 0) rc = serchr('\015');
  478.     if (rc == 0) rc = serchr('\012');
  479.     dbgwait();
  480.     }
  481. else {
  482.     if (!debugopnflg) {
  483.         FSDelete(DEBUGFILE, 0);
  484.         Create(DEBUGFILE, 0, 'MPS ', 'TEXT');
  485.         debugopnflg = 1;
  486.         }
  487.     if (FSOpen(DEBUGFILE, 0, &fref) != 0) return;
  488.     SetFPos(fref, fsFromLEOF, 0L);
  489.     count = strlen(s);
  490.     if (count > 0) FSWrite(fref, &count, s);
  491.     count = 1;
  492.     FSWrite(fref, &count, "\015");
  493.     FSClose(fref);
  494.     /* FlushVol(DEBUGVOL, 0); */
  495.     }
  496. }
  497.  
  498. OSErr seropen(void)
  499. {
  500. static unsigned char outname[8];
  501. unsigned char inname[8];
  502. short refnum;
  503. char port = 0;            /* 0 = modem port, 1 = printer port */
  504. OSErr rc;
  505.  
  506. memset(&pbo, 0, sizeof(IOParam));
  507.  
  508. if (port == 0) {
  509.     strcpy(inname,".AIn");
  510.     strcpy(outname,".AOut");
  511.     pbo.ioRefNum = aoutRefNum;
  512.     }
  513. else {
  514.     strcpy(inname,".BIn");    
  515.     strcpy(outname,".BOut");
  516.     pbo.ioRefNum = boutRefNum;
  517.     }
  518. c2pstr(inname);
  519. c2pstr(outname);
  520.  
  521. rc = OpenDriver(outname, &refnum);
  522. if (rc == noErr) rc = OpenDriver(inname, &refnum);
  523.  
  524. pbo.ioNamePtr = (StringPtr)outname;
  525. pbo.ioVersNum = 0;
  526. pbo.ioPermssn = fsWrPerm;
  527. pbo.ioMisc = 0;
  528. pbo.ioBuffer = (Ptr)&pbchr;
  529. pbo.ioReqCount = 1;
  530. pbo.ioPosMode = 0;
  531. pbo.ioPosOffset = 0;
  532.  
  533. return(rc);
  534. }
  535.  
  536. OSErr serchr(unsigned char c)
  537. {
  538. pbchr = c;
  539. return(PBWrite((ParmBlkPtr)&pbo, 0));
  540. }
  541.  
  542. void dbgdump(unsigned char *msg, unsigned char *data, short len)
  543. {
  544. short count, i, j;
  545. unsigned char dbgchar[66];
  546.  
  547. sprintf(dbgmsg, "%s: %d bytes:", msg, len); 
  548. putln(dbgmsg);
  549.  
  550. count = 0;
  551. dbgmsg[0] = 0;
  552. dbgchar[0] = 0;
  553. for (i=0; i < len; i++) {
  554.     j = strlen(dbgmsg);
  555.     sprintf(dbgmsg+j, "%02x", data[i]);
  556.     if (xtabh != 0) {
  557.         j = strlen(dbgchar);
  558.         sprintf(dbgchar+j, "%c ", (*xtabh)[data[i]]);
  559.         }
  560.     count++;
  561.     if (count == 32) {
  562.         putln(dbgmsg);
  563.         if (xtabh != 0) putln(dbgchar);
  564.         dbgmsg[0] = 0;
  565.         dbgchar[0] = 0;
  566.         count = 0;
  567.         }
  568.     }
  569. if (strlen(dbgmsg) > 0) putln(dbgmsg);
  570. if ((strlen(dbgchar) > 0) && (xtabh != 0)) putln(dbgchar);
  571. putln("");
  572. }
  573.  
  574. void dbgwait(void)
  575. {
  576. static unsigned long *Ticks = (unsigned long *)0x16a;
  577. unsigned long limit;
  578.  
  579. limit = (*Ticks) + 15;
  580. while (limit > (*Ticks)) if (havetcp) myStask();
  581. }
  582.